In [125]:
import numpy as np
#傅里叶模块
import scipy.fftpack as fftpack
# 将时域转换成频域
import matplotlib.pyplot as plt
%matplotlib inline 
In [126]:
登月图片消噪
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-126-07f4d12e3557> in <module>()
----> 1 登月图片消噪

NameError: name '登月图片消噪' is not defined
In [127]:
scipy.fftpack模块用来计算快速傅里叶变换  
速度比传统傅里叶变换更快是对之前算法的改进  
图片是二维数据注意使用fftpack的二维转变方法
  File "<ipython-input-127-af31c4de2367>", line 2
    速度比传统傅里叶变换更快,是对之前算法的改进
                         ^
SyntaxError: invalid character in identifier
In [128]:
moon = plt.imread('moonlanding.png')
plt.figure(figsize=(12,9))
plt.imshow(moon,cmap='gray')
Out[128]:
<matplotlib.image.AxesImage at 0x24027b13518>
In [129]:
### 噪声----频率高
#使用傅里叶从时域---》频域-----》过滤碍眼的高频波
In [130]:
moon.shape
Out[130]:
(474, 630)
In [131]:
moon_fft = fftpack.fft2(moon)
moon_fft
Out[131]:
array([[126598.45      +0.       j,  -4608.5796 -1892.4688   j,
          -322.093    -20.27744  j, ...,   -906.1585 +1539.3081   j,
          -322.093    +20.27744  j,  -4608.5796 +1892.4688   j],
       [ -9421.1    +5242.1133   j,   5224.016  -3171.7434   j,
          1607.9927 +1269.4243   j, ...,   -677.34503 -936.16174  j,
           354.6247 -1003.8348   j,   1965.366  -2188.0593   j],
       [ -2928.3513 +7280.916    j,  -1116.4065 +1338.3179   j,
          -474.20056 +385.40216  j, ...,    239.7723  -977.2129   j,
          1582.9283  -261.95346  j,   2641.927   -292.09366  j],
       ...,
       [  1850.5718 -2451.1787   j,   -781.0807   +13.744501 j,
           377.90707  +12.6699295j, ...,  -1526.7869 +1271.2621   j,
         -2705.5718 -3488.529    j,   1897.404  -2281.9092   j],
       [ -2928.3513 -7280.916    j,   2641.927   +292.09366  j,
          1582.9283  +261.95346  j, ...,  -2208.4302   +81.807434 j,
          -474.20056 -385.40216  j,  -1116.4065 -1338.3179   j],
       [ -9421.1    -5242.1133   j,   1965.366  +2188.0593   j,
           354.6247 +1003.8348   j, ...,   1190.5856 -1431.9937   j,
          1607.9927 -1269.4243   j,   5224.016  +3171.7434   j]],
      dtype=complex64)
In [132]:
cond = np.abs(moon_fft)>1e3
# 高频波过滤掉
moon_fft[cond] = 0
In [133]:
# 傅里叶反转
moon_result = fftpack.ifft2(moon_fft)
moon_result
Out[133]:
array([[-0.2826645 +2.33557677e-19j,  0.08010893-4.71962655e-17j,
        -0.21303105-4.89892796e-18j, ..., -0.03895291-6.19935208e-17j,
        -0.18217105+1.34156767e-17j, -0.17954478-1.03165445e-17j],
       [-0.00702763-1.48111269e-18j,  0.06715892-3.05635560e-17j,
        -0.09148968+4.19176174e-18j, ..., -0.1856927 +7.25142325e-17j,
        -0.19348538+8.73479169e-17j, -0.15636131+6.12678386e-17j],
       [-0.03724453-3.35300143e-18j, -0.12469123+1.08218724e-17j,
        -0.10263439-1.19673960e-17j, ..., -0.18584874+7.60105198e-17j,
        -0.19098356+4.52617680e-17j,  0.08034539-1.52218915e-16j],
       ...,
       [ 0.07167082-1.52428939e-17j, -0.04644684-2.88242851e-18j,
        -0.07860907+4.24181758e-17j, ...,  0.09754767-1.00951231e-17j,
         0.02351329+7.80923746e-17j,  0.18534262-1.73418376e-16j],
       [ 0.00943622+1.78454440e-17j, -0.06645724+5.35016450e-17j,
        -0.02696014+4.33941762e-17j, ...,  0.05524265+4.29780679e-17j,
        -0.00268553+3.85546799e-17j,  0.19484597-1.19051870e-16j],
       [-0.0551862 +1.26919177e-17j,  0.03641299+4.39292173e-18j,
        -0.06190041+8.31218329e-18j, ...,  0.03356535+3.10302877e-17j,
         0.02719305+5.16000957e-17j, -0.02497441+3.88575604e-17j]],
      dtype=complex64)
In [134]:
moon_result = np.real(moon_result)
moon_result
Out[134]:
array([[-0.2826645 ,  0.08010893, -0.21303105, ..., -0.03895291,
        -0.18217105, -0.17954478],
       [-0.00702763,  0.06715892, -0.09148968, ..., -0.1856927 ,
        -0.19348538, -0.15636131],
       [-0.03724453, -0.12469123, -0.10263439, ..., -0.18584874,
        -0.19098356,  0.08034539],
       ...,
       [ 0.07167082, -0.04644684, -0.07860907, ...,  0.09754767,
         0.02351329,  0.18534262],
       [ 0.00943622, -0.06645724, -0.02696014, ...,  0.05524265,
        -0.00268553,  0.19484597],
       [-0.0551862 ,  0.03641299, -0.06190041, ...,  0.03356535,
         0.02719305, -0.02497441]], dtype=float32)
In [135]:
plt.figure(figsize=(12,9))
plt.imshow(moon_result,cmap='gray')
Out[135]:
<matplotlib.image.AxesImage at 0x24027ea14e0>
In [136]:
数值积分求解圆周率
求解圆周率

integrate 对函数(1 - x^2)^0.5进行积分
  File "<ipython-input-136-b9d0c349dac9>", line 1
    数值积分,求解圆周率
             ^
SyntaxError: invalid character in identifier
In [137]:
x = np.linspace(-1,1,1000)
In [138]:
f = lambda x: (1-x**2)**0.5
In [139]:
# 画一个圆
plt.figure(figsize=(4,4))
plt.plot(x,f(x))
plt.plot(x,-f(x))
Out[139]:
[<matplotlib.lines.Line2D at 0x24027ea1898>]
In [140]:
圆的面积是

使用scipy.integrate进行积分调用quad()方法
  File "<ipython-input-140-4fbdb3b82095>", line 1
    圆的面积是?
         ^
SyntaxError: invalid character in identifier
In [141]:
import scipy.integrate as integrate
In [142]:
square,error = integrate.quad(f,-1,1)
print('圆周率是%s,误差%s'%(square*2,error))
圆周率是3.141592653589797,误差1.0002356720661965e-09
In [143]:
Scipy文件输入/输出
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-143-04496ad126d1> in <module>()
----> 1 Scipy文件输入/输出

NameError: name 'Scipy文件输入' is not defined
In [144]:
import scipy.io as io
In [145]:
随机生成数组使用scipy中的io.savemat()保存
文件格式是.mat标准的二进制文件
  File "<ipython-input-145-2915b38b4710>", line 1
    随机生成数组,使用scipy中的io.savemat()保存
                     ^
SyntaxError: invalid character in identifier
In [146]:
# matrix 矩阵
# mat标准的二进制文件
# matlab软件,非常强大
io.savemat('daata.mat',mdict={'python':np.random.randint(1,9,size=(4,5))})
In [147]:
使用io.loadmat()读取数据
  File "<ipython-input-147-28f6f5c1140c>", line 1
    使用io.loadmat()读取数据
                     ^
SyntaxError: invalid syntax
In [148]:
data = io.loadmat('./daata.mat')
data['python']
Out[148]:
array([[8, 6, 4, 7, 7],
       [6, 8, 8, 5, 2],
       [4, 3, 6, 6, 4],
       [6, 4, 4, 5, 7]])
In [149]:
读写图片使用scipy中misc.imread()/imsave()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-149-af7c8a12aa4b> in <module>()
----> 1 读写图片使用scipy中misc.imread()/imsave()

NameError: name '读写图片使用scipy中misc' is not defined
In [150]:
import scipy.misc as misc
import scipy as scipy
scipy.__version__
Out[150]:
'1.0.0'
In [151]:
cat = misc.imread('./cat.jpg')
cat
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: `imread` is deprecated!
`imread` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``imageio.imread`` instead.
  """Entry point for launching an IPython kernel.
Out[151]:
array([[[231, 186, 131],
        [232, 187, 132],
        [233, 188, 133],
        ...,
        [100,  54,  54],
        [ 92,  48,  47],
        [ 85,  43,  44]],

       [[232, 187, 132],
        [232, 187, 132],
        [233, 188, 133],
        ...,
        [100,  54,  54],
        [ 92,  48,  47],
        [ 84,  42,  43]],

       [[232, 187, 132],
        [233, 188, 133],
        [233, 188, 133],
        ...,
        [ 99,  53,  53],
        [ 91,  47,  46],
        [ 83,  41,  42]],

       ...,

       [[199, 119,  82],
        [199, 119,  82],
        [200, 120,  83],
        ...,
        [189,  99,  65],
        [187,  97,  63],
        [187,  97,  63]],

       [[199, 119,  82],
        [199, 119,  82],
        [199, 119,  82],
        ...,
        [188,  98,  64],
        [186,  96,  62],
        [188,  95,  62]],

       [[199, 119,  82],
        [199, 119,  82],
        [199, 119,  82],
        ...,
        [188,  98,  64],
        [188,  95,  62],
        [188,  95,  62]]], dtype=uint8)
In [152]:
misc.imshow(cat)
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: `imshow` is deprecated!
`imshow` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``matplotlib.pyplot.imshow`` instead.
  """Entry point for launching an IPython kernel.
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-152-e229d38c4557> in <module>()
----> 1 misc.imshow(cat)

D:\Anaconda3\lib\site-packages\numpy\lib\utils.py in newfunc(*args, **kwds)
     99             """`arrayrange` is deprecated, use `arange` instead!"""
    100             warnings.warn(depdoc, DeprecationWarning, stacklevel=2)
--> 101             return func(*args, **kwds)
    102 
    103         newfunc = _set_function_name(newfunc, old_name)

D:\Anaconda3\lib\site-packages\scipy\misc\pilutil.py in imshow(arr)
    504     os.unlink(fname)
    505     if status != 0:
--> 506         raise RuntimeError('Could not execute image viewer.')
    507 
    508 

RuntimeError: Could not execute image viewer.
In [153]:
misc旋转resizeimfilter
  File "<ipython-input-153-a4a8ff01968b>", line 1
    misc旋转、resize、imfilter
                         ^
SyntaxError: invalid character in identifier
In [154]:
cat_rotate = misc.imrotate(cat,60)
cat_rotate
misc.imshow(cat_rotate)
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: `imrotate` is deprecated!
`imrotate` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.rotate`` instead.
  """Entry point for launching an IPython kernel.
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: DeprecationWarning: `imshow` is deprecated!
`imshow` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``matplotlib.pyplot.imshow`` instead.
  This is separate from the ipykernel package so we can avoid doing imports until
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-154-9f94e676a8d5> in <module>()
      1 cat_rotate = misc.imrotate(cat,60)
      2 cat_rotate
----> 3 misc.imshow(cat_rotate)

D:\Anaconda3\lib\site-packages\numpy\lib\utils.py in newfunc(*args, **kwds)
     99             """`arrayrange` is deprecated, use `arange` instead!"""
    100             warnings.warn(depdoc, DeprecationWarning, stacklevel=2)
--> 101             return func(*args, **kwds)
    102 
    103         newfunc = _set_function_name(newfunc, old_name)

D:\Anaconda3\lib\site-packages\scipy\misc\pilutil.py in imshow(arr)
    504     os.unlink(fname)
    505     if status != 0:
--> 506         raise RuntimeError('Could not execute image viewer.')
    507 
    508 

RuntimeError: Could not execute image viewer.
In [155]:
cat_resize = misc.imresize(cat,size=50)
misc.imshow(cat_resize)
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``skimage.transform.resize`` instead.
  """Entry point for launching an IPython kernel.
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DeprecationWarning: `imshow` is deprecated!
`imshow` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``matplotlib.pyplot.imshow`` instead.
  
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-155-9bd4829cc7f8> in <module>()
      1 cat_resize = misc.imresize(cat,size=50)
----> 2 misc.imshow(cat_resize)

D:\Anaconda3\lib\site-packages\numpy\lib\utils.py in newfunc(*args, **kwds)
     99             """`arrayrange` is deprecated, use `arange` instead!"""
    100             warnings.warn(depdoc, DeprecationWarning, stacklevel=2)
--> 101             return func(*args, **kwds)
    102 
    103         newfunc = _set_function_name(newfunc, old_name)

D:\Anaconda3\lib\site-packages\scipy\misc\pilutil.py in imshow(arr)
    504     os.unlink(fname)
    505     if status != 0:
--> 506         raise RuntimeError('Could not execute image viewer.')
    507 
    508 

RuntimeError: Could not execute image viewer.
In [156]:
# 第二个参数表示风格
cat_filter = misc.imfilter(cat,'emboss')
misc.imshow(cat_filter)
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: DeprecationWarning: `imfilter` is deprecated!
`imfilter` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use Pillow filtering functionality directly.
  
D:\Anaconda3\lib\site-packages\ipykernel_launcher.py:3: DeprecationWarning: `imshow` is deprecated!
`imshow` is deprecated in SciPy 1.0.0, and will be removed in 1.2.0.
Use ``matplotlib.pyplot.imshow`` instead.
  This is separate from the ipykernel package so we can avoid doing imports until
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-156-c1b4472fa49f> in <module>()
      1 # 第二个参数表示风格
      2 cat_filter = misc.imfilter(cat,'emboss')
----> 3 misc.imshow(cat_filter)

D:\Anaconda3\lib\site-packages\numpy\lib\utils.py in newfunc(*args, **kwds)
     99             """`arrayrange` is deprecated, use `arange` instead!"""
    100             warnings.warn(depdoc, DeprecationWarning, stacklevel=2)
--> 101             return func(*args, **kwds)
    102 
    103         newfunc = _set_function_name(newfunc, old_name)

D:\Anaconda3\lib\site-packages\scipy\misc\pilutil.py in imshow(arr)
    504     os.unlink(fname)
    505     if status != 0:
--> 506         raise RuntimeError('Could not execute image viewer.')
    507 
    508 

RuntimeError: Could not execute image viewer.

图片处理

使用scipy.misc.face(gray=True)获取图片,使用ndimage移动坐标、旋转图片、切割图片、缩放图片

导包,读取图片显示图片

In [181]:
import scipy.ndimage as ndimage
In [246]:
# scipy提供了一张模板图片
face = misc.face(gray=True)
print(type(face))
# plt.imshow(face.mean(axis=-1),cmap='gray')
plt.imshow(face,cmap='gray')
<class 'numpy.ndarray'>
Out[246]:
<matplotlib.image.AxesImage at 0x240235132e8>
In [159]:
shift移动坐标
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-159-f2ad3bffa5eb> in <module>()
----> 1 shift移动坐标

NameError: name 'shift移动坐标' is not defined
In [183]:
face1 = ndimage.shift(face,shift=[100,200,0])
plt.imshow(face1,cmap='gray')
Out[183]:
<matplotlib.image.AxesImage at 0x24028510f28>
In [186]:
face1 = ndimage.shift(face,shift=[100,200,0],cval=255)
plt.imshow(face1,cmap='gray')
Out[186]:
<matplotlib.image.AxesImage at 0x240296f2f98>
In [188]:
'''mode : str, optional
    Points outside the boundaries of the input are filled according
    to the given mode ('constant', 'nearest', 'reflect', 'mirror' or 'wrap').
    Default is 'constant'.'''

face1 = ndimage.shift(face,shift=[380,0,1],mode='mirror')
plt.imshow(face1,cmap='gray')
Out[188]:
<matplotlib.image.AxesImage at 0x24029afbcf8>
In [ ]:
zoom缩放图片
In [189]:
#旋转
face2 = ndimage.rotate(face,90)
plt.imshow(face2,cmap='gray')
Out[189]:
<matplotlib.image.AxesImage at 0x24029b5cf60>
In [192]:
face3 = ndimage.zoom(face,zoom=[0.5,0.3,1])
plt.imshow(face3,cmap='gray')
Out[192]:
<matplotlib.image.AxesImage at 0x24029e8d7f0>
In [166]:
切割图片
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-166-6a6306950aa3> in <module>()
----> 1 切割图片

NameError: name '切割图片' is not defined
In [193]:
face2 = ndimage.rotate(face,90)
plt.imshow(face2,cmap='gray')
Out[193]:
<matplotlib.image.AxesImage at 0x24029ef12b0>
In [194]:
face4 = face[:512,-512:]
plt.imshow(face4)
Out[194]:
<matplotlib.image.AxesImage at 0x24029f47320>
In [169]:
图片进行过滤
添加噪声对噪声图片使用ndimage中的高斯滤波中值滤波signal中维纳滤波进行处理
使图片变清楚
  File "<ipython-input-169-b48be2bfe223>", line 2
    添加噪声,对噪声图片使用ndimage中的高斯滤波、中值滤波、signal中维纳滤波进行处理
                                                 ^
SyntaxError: invalid character in identifier
In [195]:
moon = plt.imread('./moonlanding.png')
plt.figure(figsize=(12,9))
plt.imshow(moon,cmap='gray')
Out[195]:
<matplotlib.image.AxesImage at 0x24027feceb8>
In [170]:
加载图片使用灰色图片misc.face()添加噪声
  File "<ipython-input-170-52da6d240c8d>", line 1
    加载图片,使用灰色图片misc.face()添加噪声
                  ^
SyntaxError: invalid character in identifier
In [196]:
plt.imshow(face4)
Out[196]:
<matplotlib.image.AxesImage at 0x2402804c5c0>
In [247]:
# 这个是对face的gray=True的情况做的操作
face5 = face+face.std() * np.random.randn(face.shape[0],face.shape[1]) * 0.3
# face5 = face+face.std()*np.random.randn(face.shape[0],face.shape[1])*0.3
plt.imshow(face5,cmap='gray')
face.shape[0]
Out[247]:
768
In [240]:
face.shape[0]
Out[240]:
768
In [248]:
face6 = ndimage.gaussian_filter(face5,sigma=1)
plt.figure(figsize=(12,4))
# 展示三章图片,一行3列
axes = plt.subplot(1,3,1)
axes.imshow(face4,cmap='gray')
axes = plt.subplot(1,3,2)
axes.imshow(face5,cmap='gray')
axes = plt.subplot(1,3,3)
axes.imshow(face6,cmap='gray')
Out[248]:
<matplotlib.image.AxesImage at 0x2402360dc18>
In [174]:
高斯滤波sigma高斯核的标准偏差
  File "<ipython-input-174-d4f4495298f3>", line 1
    高斯滤波sigma:高斯核的标准偏差
                     ^
SyntaxError: invalid character in identifier
In [175]:
moon_gaussian = ndimage.gaussian_filter(moon,sigma=2.5)
plt.figure(figsize=(12,9))
plt.imshow(moon_gaussian,cmap='gray')
Out[175]:
<matplotlib.image.AxesImage at 0x24027f78c18>
In [176]:
中值滤波参数size给出在每个元素上从输入数组中取出的形状位置定义过滤器功能的输入
  File "<ipython-input-176-a1ef7ca17bcf>", line 1
    中值滤波参数size:给出在每个元素上从输入数组中取出的形状位置,定义过滤器功能的输入
                                              ^
SyntaxError: invalid character in identifier
In [249]:
face7 = ndimage.median_filter(face5,size=5)
plt.figure(figsize=(12,4))
# 展示三章图片,一行3列
axes = plt.subplot(1,3,1)
axes.imshow(face4,cmap='gray')
axes = plt.subplot(1,3,2)
axes.imshow(face5,cmap='gray')
axes = plt.subplot(1,3,3)
axes.imshow(face7,cmap='gray')
Out[249]:
<matplotlib.image.AxesImage at 0x24023712588>
In [178]:
signal维纳滤波mysize滤镜尺寸的标量
  File "<ipython-input-178-5fa522f9fbcc>", line 1
    signal维纳滤波mysize:滤镜尺寸的标量
                           ^
SyntaxError: invalid character in identifier
In [179]:
import scipy.signal as signal
In [250]:
face8 = signal.wiener(face5,mysize=5)
plt.figure(figsize=(12,4))
# 展示三章图片,一行3列
# 参数会自动将数值隔开
axes = plt.subplot(131)
axes.imshow(face4,cmap='gray')
axes = plt.subplot(132)
axes.imshow(face5,cmap='gray')
axes = plt.subplot(133)
axes.imshow(face8,cmap='gray')
Out[250]:
<matplotlib.image.AxesImage at 0x24029966e48>